extern crate serde_json;

use self::serde_json::{json, Value};
use super::{cur_time, do_cache, do_cache_ret, do_node, do_node_ret};
use base::base::{VBase, VMap};

#[allow(dead_code)]
pub async fn ck_goods(p: String, val: Value) {
    let cmd = vec!["all", "year", "te1", "te2", "psn"];

    let val = val.kobj().unwrap();

    for d in cmd.iter() {
        let m = goods_cmd(d);

        match val.k_val(d) {
            Some(v) => {
                do_node_ret(p.clone(), m.clone(), json!(v)).await;
                do_cache_ret(p.clone(), m, json!(v)).await;
            }
            None => {
                do_node(p.clone(), m.clone()).await;
                do_cache(p.clone(), m).await;
            }
        }
    }
}

#[allow(dead_code)]
pub fn goods_cmd(cmd: &str) -> Value {
    let y = cur_time("%Y");

    match cmd {
        "all" => json!({"line": "1", "tp": "goods", "sub": "all"}),
        "year" => json!({"line": "1", "tp": "goods", "sub": "year", "sn": y}),
        "te1" | "te2" => json!({"line": "1", "tp": "goods", "sn": cmd}),
        "psn" => json!({"line": "1", "tp": "goods", "sub": "psn", "sn": "te1"}),
        _ => panic!("goods_cmd"),
    }
}

#[allow(dead_code)]
pub fn goods_data() -> Value {
    let pstart = cur_time("%Y-%m-%d 00:00:00");
    let y = cur_time("%Y");

    json!({
        "all": [y],
        "year_1": ["te1"],
        "year_2": ["te2"],
        "year": ["te1", "te2"],
        "wsn": ["M010", "M020"],
        "wsn_1": ["M010"],
        "wsn_2": ["M020"],
        "te1": {
            "sn": "te1",
            "name": "name_te1",
            "pstart": pstart,
        },
        "te2": {
            "sn": "te2",
            "name": "name_te2",
            "pstart": pstart,
        },
        "c_te1": {
            "sn": "te1",
            "name": "n_te1",
            "pstart": pstart,
            "task": 1,
        },
        "c_te2": {
            "sn": "te2",
            "name": "n_te2",
            "pstart": pstart,
        },
        "c_psn1": {
            "sn": "psn1",
            "name": "name_psn1",
            "task": "te1",
        },
        "psn": ["psn1"],
    })
}
